#include "OptionsState.h"

#include "Game.h"
#include "MainMenuState.h"
#include "tinyxml.h"

void OptionsState::Enter() 
{
	// Getting bitmap font
	GroupFont =	CBitmapFont::GetInstance();

	// Load bitmap font
	FontImageID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Robovox.bmp"), D3DCOLOR_XRGB(0,0,0));

	// Set the bitmap font's values
	GroupFont->SetValues(32, 32, 16, 14, ' ', FontImageID);

	// Cursor position
	cursorY = 150;
	cursorX = 640;

	// Sound information
	musicVolume = MainMenuState::GetInstance()->GetMusicVolume();
	SFXVolume = MainMenuState::GetInstance()->GetSFXVolume();

	// Timer
	seanTimer = 0;
}

void OptionsState::Exit()
{
	// Unload bitmap font
	if(FontImageID != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(FontImageID);
		FontImageID = -1;
	}

	seanTimer = 0;

	MainMenuState::GetInstance()->SetMusicVolume(musicVolume);
	MainMenuState::GetInstance()->SetSFXVolume(SFXVolume);
	SaveSounds("SoundData.xml");
}

bool OptionsState::Input() 
{
	// If escape return to main menu
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_ESCAPE) ||CSGD_DirectInput::GetInstance()->JoystickButtonPressed(6))
		cursorY = 450;

	// Move cursor up
	if((CSGD_DirectInput::GetInstance()->KeyPressed(DIK_UP) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_UP)) && cursorY != 150)
		cursorY -= 100;
	else if((CSGD_DirectInput::GetInstance()->KeyPressed(DIK_UP) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_UP)) && cursorY == 150)
		cursorY = 450;

	// Move cursor down
	if((CSGD_DirectInput::GetInstance()->KeyPressed(DIK_DOWN) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_DOWN)) && cursorY != 450)
		cursorY += 100;
	else if((CSGD_DirectInput::GetInstance()->KeyPressed(DIK_DOWN) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_DOWN)) && cursorY == 450)
		cursorY = 150;

	// Move cursor left or right, if on full screen option
	if((CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RIGHT) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_RIGHT)) && cursorY == 150 && cursorX != 740)
		cursorX += 100;
	else if((CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RIGHT) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_RIGHT)) && cursorY == 150 && cursorX == 740)
		cursorX = 640;
	if((CSGD_DirectInput::GetInstance()->KeyPressed(DIK_LEFT) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_LEFT)) && cursorY == 150 && cursorX != 640)
		cursorX -= 100;
	else if((CSGD_DirectInput::GetInstance()->KeyPressed(DIK_LEFT) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_LEFT)) && cursorY == 150 && cursorX == 640)
		cursorX = 740;

	// Change full screen
	if((CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RETURN) || CSGD_DirectInput::GetInstance()->JoystickButtonPressed(0)) && cursorY == 150 && cursorX == 640)
	{
		Game::GetInstance()->SetFullScreen(false);
		Game::GetInstance()->FullScreenOn();
	}
	else if((CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RETURN) || CSGD_DirectInput::GetInstance()->JoystickButtonPressed(0)) && cursorY == 150 && cursorX == 740)
	{
		Game::GetInstance()->SetFullScreen(true);
		Game::GetInstance()->FullScreenOn();
	}

	// Change music
	if(cursorY == 250 && (CSGD_DirectInput::GetInstance()->KeyDown(DIK_RIGHT) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_RIGHT)) && musicVolume <= 99)
	{
		seanTimer += 0.005;
		if(seanTimer >= 1)
		{
			musicVolume += 1;
			seanTimer = 0;
		}
	}
	if(cursorY == 250 && (CSGD_DirectInput::GetInstance()->KeyDown(DIK_LEFT) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_LEFT)) && musicVolume >= 1)
	{
		seanTimer += 0.005;
		if(seanTimer >= 1)
		{
			musicVolume -= 1;
			seanTimer = 0;
		}
	}

	// Change SFX
	if(cursorY == 350 && (CSGD_DirectInput::GetInstance()->KeyDown(DIK_RIGHT) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_RIGHT)) && SFXVolume <= 99)
	{
		seanTimer += 0.005;
		if(seanTimer >= 1)
		{
			SFXVolume += 1;
			seanTimer = 0;
		}
	}
	if(cursorY == 350 && (CSGD_DirectInput::GetInstance()->KeyDown(DIK_LEFT) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_LEFT)) && SFXVolume >= 1)
	{
		seanTimer += 0.005;
		if(seanTimer >= 1)
		{
			SFXVolume -= 1;
			seanTimer = 0;
		}
	}

	// Go back to the main menu
	if((CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RETURN) || CSGD_DirectInput::GetInstance()->JoystickButtonPressed(0)) && cursorY == 450)
		Game::GetInstance()->ChangeState(MainMenuState::GetInstance());

	return true;
}

void OptionsState::Update(float elapsed)
{
}

void OptionsState::Render() 
{
	// Position of music and sfx on the screen
	int musicPosition = 500+musicVolume*3;
	int SFXPositon = 500+SFXVolume*3;

	// Selected and not selected colors
	DWORD isSelected = D3DCOLOR_ARGB(255,255,0,0);
	DWORD notSelected =  D3DCOLOR_ARGB(255,0,255,255);

	// Title of options
	GroupFont->Print("Options", 32, 32, 3.0f, D3DCOLOR_ARGB(255,0,255,255));

	// Options menu
	GroupFont->Print("Full Screen: ", 340-100, 150,1.0f, notSelected);
	GroupFont->Print("On", 640-100, 150, 1.0f, cursorY==150 && cursorX==640 ? isSelected : notSelected);
	GroupFont->Print("Off", 740-100, 150, 1.0f, cursorY==150 && cursorX==740 ? isSelected : notSelected);
	GroupFont->Print("Music:", 340-100, 250, 1.0f, cursorY==250 ? isSelected : notSelected);
	GroupFont->Print("SFX:", 340-100, 350, 1.0f, cursorY==350 ? isSelected : notSelected);
	GroupFont->Print("Quit", 340-100, 450, 1.0f, cursorY==450 ? isSelected : notSelected);

	// Draw lines and box for music and sfx
	RECT musicBox;
	musicBox.left = musicPosition-10-100;
	musicBox.right = musicPosition+10-100;
	musicBox.top = 240;
	musicBox.bottom = 260;
	RECT SFXBox;
	SFXBox.left = SFXPositon-10-100;
	SFXBox.right = SFXPositon+10-100;
	SFXBox.top = 340;
	SFXBox.bottom = 360;
	CSGD_Direct3D::GetInstance()->DrawLine(500-100,250,800-100,250,0,255,255);
	CSGD_Direct3D::GetInstance()->DrawLine(500-100,350,800-100,350,0,255,255);
	CSGD_Direct3D::GetInstance()->DrawRect(musicBox, cursorY==250 ? 255 : 0, cursorY==250 ? 0 : 255, cursorY==250 ? 0 : 255);
	CSGD_Direct3D::GetInstance()->DrawRect(SFXBox, cursorY==350 ? 255 : 0, cursorY==350 ? 0 : 255, cursorY==350 ? 0 : 255);
}

OptionsState::OptionsState() 
{
}

OptionsState::OptionsState(OptionsState& menu) 
{
}

OptionsState* OptionsState::GetInstance() 
{
	static OptionsState instance;
	return &instance;
}

OptionsState::~OptionsState()
{

}

void OptionsState::SaveSounds(const char* szFileName)
{
	// Create a TinyXML Document
	TiXmlDocument doc;

	// Create a TinyXML Declaration (version info)
	TiXmlDeclaration* pDec = new TiXmlDeclaration("1.0","utf-8","");
	// Add the declaration to the document
	doc.LinkEndChild(pDec);
	
	// Create a root node for the document (sound_list)
	TiXmlElement* pRoot = new TiXmlElement("sound_list");
	// Add the root node to the document
	doc.LinkEndChild(pRoot);

	// Create a node for the sounds
	TiXmlElement* pSound = new TiXmlElement("sound_info");

	// Fill the node with sounds info
	pSound->SetAttribute("music",musicVolume);
	pSound->SetAttribute("sfx",SFXVolume);

	// Add the sounds node to the "sounds_list"
	pRoot->LinkEndChild(pSound);

	// Save the XML document to the file
	doc.SaveFile(szFileName);
}